﻿/*******************************************************************************
Copyright (c) 2011-2013, PKStudio
Copyright (c) 2011,      Zynga Inc.
Copyright (c) 2008-2010, Ricardo Quesada

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Diagnostics;
using System.Drawing;

namespace PKEngine
{
	/// <summary>
	/// Base class for PKTransition scenes.
	/// </summary>
	public class PKTransitionScene : PKScene
	{
		#region Private Fields
		protected PKScene inScene;
		protected PKScene outScene;
		private bool needCleanupScene;
		#endregion

		#region Public Properties
		public float Duration { get; private set; }
		#endregion

		#region Initialization
		public PKTransitionScene(float duration, PKScene scene)
			: base()
		{
			Debug.Assert(scene != null, "PKTransitionScene: Argument cannot be null.");

			this.Duration = duration;

			this.inScene = scene;
			this.outScene = PKGame.SharedGame.RunningScene;
			Debug.Assert(inScene != outScene, "PKTransitionScene: Incoming scene must be different from the outgoing scene.");
		}
		#endregion

		#region Public Methods
		public override void Draw()
		{
			base.Draw();

			outScene.Visit();
			inScene.Visit();
		}

		public override void OnEnter()
		{
			base.OnEnter();

			PKMouseDispatcher.SharedDispatcher.DispatchEvents = false;
			PKKeybdDispatcher.SharedDispatcher.DispatchEvents = false;

			outScene.OnExitTransitionDidStart();
			inScene.OnEnter();
		}

		public override void OnExit()
		{
			base.OnExit();

			PKMouseDispatcher.SharedDispatcher.DispatchEvents = true;
			PKKeybdDispatcher.SharedDispatcher.DispatchEvents = true;

			outScene.OnExit();
			inScene.OnEnterTransitionDidFinish();
		}

		public override void Cleanup()
		{
			base.Cleanup();

			if (needCleanupScene)
			{
				outScene.Cleanup();
			}
		}

		/// <summary>
		/// Called after the transition finishes.
		/// </summary>
		public void Finish()
		{
			inScene.IsVisible = true;
			inScene.Position = Point.Empty;
			outScene.IsVisible = false;
			outScene.Position = Point.Empty;

			this.ScheduleOnce(new SchedulerDelegate(this.SetNewScene), 0);
		}

		/// <summary>
		/// Used by some transitions to hide the outter scene.
		/// </summary>
		public void HideOutShowIn()
		{
			inScene.IsVisible = true;
			outScene.IsVisible = false;
		}
		#endregion

		#region Private Methods
		private void SetNewScene(float dt)
		{
			needCleanupScene = PKGame.SharedGame.NeedCleanupScene;
			PKGame.SharedGame.ReplaceScene(inScene);

			outScene.IsVisible = true;
		}
		#endregion
	}

	/// <summary>
	/// Fade out the outgoing scene and then fade in the incoming scene.
	/// </summary>
	public class PKTransitionFade : PKTransitionScene
	{
		#region Public Properties
		public ushort Color { get; private set; }
		#endregion

		#region Initialization
		public PKTransitionFade(float duration, PKScene scene)
			: this(duration, scene, PKAlpha.SharedAlpha.ColorBlack)
		{
		}

		public PKTransitionFade(float duration, PKScene scene, ushort color)
			: base(duration, scene)
		{
			this.Color = color;
		}
		#endregion

		#region Public Methods
		public override void OnEnter()
		{
			base.OnEnter();

			PKLayerColor layer = new PKLayerColor(Color) { Opacity = 0 };
			this.AddChild(layer, 1);

			inScene.IsVisible = false;

			PKActionInterval action = new PKSequence(new PKFiniteTimeAction[]
			                                         {
			                                         	new PKFadeIn(Duration / 2),
			                                         	new PKCallFunc(new CallFuncDelegate(this.HideOutShowIn)),
			                                         	new PKFadeOut(Duration / 2),
			                                         	new PKCallFunc(new CallFuncDelegate(this.Finish))
			                                         });
			layer.RunAction(action);
		}

		public override void OnExit()
		{
			base.OnExit();
			this.ClearChildren();
		}
		#endregion
	}

	/// <summary>
	/// Cross fades two scenes
	/// </summary>
	public class PKTransitionCrossFade : PKTransitionScene
	{
		#region Initialization
		public PKTransitionCrossFade(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void OnEnter()
		{
			base.OnEnter();

			PKSurface surface = PKMainSurface.SharedMainSurface.ScreenClone();
			PKSprite sprite = new PKSprite(surface);
			this.AddChild(sprite, 1);

			outScene.IsVisible = false;

			PKActionInterval action = new PKSequence(new PKFiniteTimeAction[]
			                                         {
			                                         	new PKFadeOut(Duration),
			                                         	new PKCallFunc(new CallFuncDelegate(this.Finish))
			                                         });
			sprite.RunAction(action);
		}

		public override void OnExit()
		{
			base.OnExit();
			this.ClearChildren();
		}
		#endregion
	}

	/// <summary>
	/// Move in from to the left the incoming scene.
	/// </summary>
	public class PKTransitionMoveInL : PKTransitionScene
	{
		#region Initialization
		public PKTransitionMoveInL(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void OnEnter()
		{
			base.OnEnter();

			this.InitScene();

			PKActionInterval action = new PKSequence(new PKFiniteTimeAction[]
			                                         {
			                                         	new PKEaseOut(new PKMoveTo(Duration, new Point(0, 0)), 2.0f),
			                                         	new PKCallFunc(new CallFuncDelegate(this.Finish))
			                                         });
			inScene.RunAction(action);
		}

		public virtual void InitScene()
		{
			inScene.Position = new Point(-PKConfig.SharedConfig.ScreenWidth, 0);
		}
		#endregion
	}

	/// <summary>
	/// Move in from to the right the incoming scene.
	/// </summary>
	public class PKTransitionMoveInR : PKTransitionMoveInL
	{
		#region Initialization
		public PKTransitionMoveInR(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(PKConfig.SharedConfig.ScreenWidth, 0);
		}
		#endregion
	}

	/// <summary>
	/// Move in from to the top the incoming scene.
	/// </summary>
	public class PKTransitionMoveInT : PKTransitionMoveInL
	{
		#region Initialization
		public PKTransitionMoveInT(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(0, -PKConfig.SharedConfig.ScreenHeight);
		}
		#endregion
	}

	/// <summary>
	/// Move in from to the bottom the incoming scene.
	/// </summary>
	public class PKTransitionMoveInB : PKTransitionMoveInL
	{
		#region Initialization
		public PKTransitionMoveInB(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(0, PKConfig.SharedConfig.ScreenHeight);
		}
		#endregion
	}

	/// <summary>
	/// Slide in the incoming scene from the left border.
	/// </summary>
	public class PKTransitionSlideInL : PKTransitionScene
	{
		#region Initialization
		public PKTransitionSlideInL(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void OnEnter()
		{
			base.OnEnter();

			this.InitScene();

			PKActionInterval outAction = new PKEaseOut(new PKMoveBy(Duration, new Point(-inScene.Position.X, -inScene.Position.Y)), 2.0f);
			PKActionInterval inAction = new PKSequence(new PKFiniteTimeAction[]
			                                         {
			                                         	new PKEaseOut(new PKMoveBy(Duration, new Point(-inScene.Position.X, -inScene.Position.Y)), 2.0f),
			                                         	new PKCallFunc(new CallFuncDelegate(this.Finish))
			                                         });
			outScene.RunAction(outAction);
			inScene.RunAction(inAction);
		}

		public virtual void InitScene()
		{
			inScene.Position = new Point(-PKConfig.SharedConfig.ScreenWidth, 0);
		}
		#endregion
	}

	/// <summary>
	/// Slide in the incoming scene from the right border.
	/// </summary>
	public class PKTransitionSlideInR : PKTransitionSlideInL
	{
		#region Initialization
		public PKTransitionSlideInR(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(PKConfig.SharedConfig.ScreenWidth, 0);
		}
		#endregion
	}

	/// <summary>
	/// Slide in the incoming scene from the top border.
	/// </summary>
	public class PKTransitionSlideInT : PKTransitionSlideInL
	{
		#region Initialization
		public PKTransitionSlideInT(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(0, -PKConfig.SharedConfig.ScreenHeight);
		}
		#endregion
	}

	/// <summary>
	/// Slide in the incoming scene from the bottom border.
	/// </summary>
	public class PKTransitionSlideInB : PKTransitionSlideInL
	{
		#region Initialization
		public PKTransitionSlideInB(float duration, PKScene scene)
			: base(duration, scene)
		{
		}
		#endregion

		#region Public Methods
		public override void InitScene()
		{
			inScene.Position = new Point(0, PKConfig.SharedConfig.ScreenHeight);
		}
		#endregion
	}

	/// <summary>
	/// Transition using a PKChannel.
	/// </summary>
	public class PKTransitionWithChannel : PKTransitionScene
	{
		#region Public Properties
		public PKChannel Channel { get; private set; }
		#endregion

		#region Initialization
		public PKTransitionWithChannel(float duration, PKScene scene, PKChannel channel)
			: base(duration, scene)
		{
			this.Channel = channel;
		}
		#endregion

		#region Public Methods
		public override void OnEnter()
		{
			base.OnEnter();

			PKSurface surface = PKMainSurface.SharedMainSurface.ScreenClone();
			PKSprite sprite = new PKSprite(surface);
			sprite.TransitionChannel = Channel;
			sprite.TransitionValue = 256;
			sprite.DisplayMode = PKSpriteDisplayMode.Transition;
			this.AddChild(sprite, 1);

			outScene.IsVisible = false;

			PKActionInterval action = new PKSequence(new PKFiniteTimeAction[]
			                                         {
			                                         	new PKTransit(Duration),
			                                         	new PKCallFunc(new CallFuncDelegate(this.Finish))
			                                         });
			sprite.RunAction(action);
		}

		public override void OnExit()
		{
			base.OnExit();
			this.ClearChildren();
		}
		#endregion
	}
}
